var WxParse = require('../../wxParse/wxParse.js');
const app = getApp();

Page({
    data: {
      id: 0,
      gua_detail: {},
      news_index: 0,
      news: {},
      news_seed: 0,

      news_list: [],  // 阁新闻列表

      preload_list: []
    },
    onLoad(options) {
      this.data.id = options.id;
      this.getGuaDetail(() => {
        this.setData({ news: this.data.gua_detail.slide[this.data.news_index] });
        this.data.news_index++;

        this.roll();

        app.rich_handle(this.data.gua_detail.content, WxParse, this);
      });

      this.newsList();
    },
    onUnload() {
      clearInterval(this.data.news_seed);
    },
    roll() {
      this.data.news_seed = setInterval(() => {
        this.setData({ news: this.data.gua_detail.slide[this.data.news_index] });
        if (this.data.news_index !== this.data.gua_detail.slide.length - 1) {
          this.data.news_index++;
        } else {
          this.data.news_index = 0;
        }
      }, 2000);
    },
    // 获取主题详情
    getGuaDetail(callback) {
      app.ajax('index/getGuaDetail', { gua_id: this.data.id }, res => {
        app.format_img_arr(res.slide);

        // 预加载
        let img_arr = app.get_column(res.slide, 'pic');
        this.setData({ preload_list: this.data.preload_list.concat(img_arr) });

        this.setData({ gua_detail: res });
        wx.setNavigationBarTitle({ title: res.title });

        if (callback) {
          callback();
        }
      });
    },
    // 获取阁新闻
    newsList() {
      let post = {
        six_id: this.data.id,
        page: 1,
        perpage: 100
      };

      app.ajax('index/newsList', post, res => {
        app.format_img_arr(res);
        this.setData({ news_list: res });
      });
    },
    // 去新闻详情
    to_news_detail(e) {
      if (e.currentTarget.dataset.url) {
        wx.navigateTo({ url: '/pages/jichu-detail/jichu-detail?id=' + e.currentTarget.dataset.url });
      }
    },
    roll_ts() {
      clearInterval(this.data.news_seed);
    },
    roll_te() {
      this.roll();
    }
  }
);